/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "queuexmlparser.h"

#include <QXmlStreamReader>

#include <QDebug>

#include "../Types/artist.h"
#include "../Types/song.h"
#include "../Types/songrequest.h"

#include "../utils.h"

bool readQueueEntry(QXmlStreamReader& xmlReader, SongRequest& sr)
{
    bool isOk = true;
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();

    while ( ( nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "entry" ) && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "entry" )
            {
                QString messageDate = xmlReader.attributes().value("request_time").toString();
                sr.requestDate = QDate::fromString(messageDate.left(10),"yyyy-MM-dd");
                sr.requestTime = QTime::fromString(messageDate.remove(0,11),"hh:mm:ss");
            }
            else if ( xmlReader.name() == "artist")
            {
                Artist artist;

                artist.id = xmlReader.attributes().value("id").toString().toUInt();
                artist.flag = xmlReader.attributes().value("flag").toString();

                isOk &= Utils::getXMLStringData(xmlReader,artist.nickname);

                sr.artists.push_back(artist);
            }
            else if ( xmlReader.name() == "song")
            {
                Song song;

                song.id = xmlReader.attributes().value("id").toString().toUInt();
                song.length = QTime::fromString(xmlReader.attributes().value("length").toString(),"m:ss");

                isOk &= Utils::getXMLStringData(xmlReader,song.title);

                sr.songInfo = song;
            }
            else if ( xmlReader.name() == "requester")
            {
                User requester;

                //requester.id = xmlReader.attributes().value("id").toString().toUInt();
                requester.flag = xmlReader.attributes().value("flag").toString();

                isOk &= Utils::getXMLStringData(xmlReader,requester.name);

                sr.requesterInfo = requester;
            }
            else
            {
                qDebug() << "Unknow node: " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    return isOk;
}

bool Parser :: readXMLQueueNow(const QByteArray& queueData, SongReqest& sr)
{
    bool isOk = true;
    QXmlStreamReader xmlReader(queueData);
    QXmlStreamReader::TokenType nodeType;

    qDebug() << "Parser :: readXMLQueueNow";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();
        //qDebug() << "Name: " << xmlReader.name() << " Type: " << nodeType;
        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "playlist" )
        {
            do
            {
                nodeType = xmlReader.readNext();

                if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "now" )
                {
                    while ( nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "now" )
                    {
                        if ( nodeType == QXmlStreamReader::StartElement )
                        {
                            if ( xmlReader.name() == "entry")
                            {
                                isOk &= readQueueEntry(xmlReader, sr);
                                sr.timeLeft = 0;
                            }
                            else if ( xmlReader.name() == "timeleft" )
                            {
                                QString timeleftString;
                                isOk &= Utils::getXMLStringData(xmlReader,timeleftString);

                                if ( isOk )
                                {
                                    sr.timeLeft = timeleftString.toInt(&isOk);
                                    if ( sr.timeLeft < 0 )
                                    {
                                        sr.timeLeft = 0;
                                    }
                                }

                                // qDebug() << "timeleft node read";
                            }
                        }
                        nodeType = xmlReader.readNext();
                    }
                }
            }while((nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "playlist") && isOk);
        }
    }

    qDebug() << sr;

    return isOk;
}

bool Parser :: readXMLQueuePending(const QByteArray& queueData, QList<SongRequest>& queue)
{
    bool isOk = true;
    QXmlStreamReader xmlReader(queueData);
    QXmlStreamReader::TokenType nodeType;

    qDebug() << "Parser :: readXMLQueuePending";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "playlist" )
        {
            do
            {
                nodeType = xmlReader.readNext();
                if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "queue" )
                {
                    while(nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "queue")
                    {
                        nodeType = xmlReader.readNext();

                        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "entry" )
                        {
                            SongRequest sr;
                            isOk &= readQueueEntry(xmlReader, sr);
                            sr.timeLeft = 0;
                            queue.push_back(sr);
                        }
                    }
                }

            }while((nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "playlist") && isOk);
        }
    }

    qDebug() << queue;

    return isOk;
}

bool Parser :: readXMLQueueHistory(const QByteArray& queueData, QList<SongRequest>& history)
{
    bool isOk = true;
    QXmlStreamReader xmlReader(queueData);
    QXmlStreamReader::TokenType nodeType;

    qDebug() << "Parser :: readXMLQueueHistory";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "playlist" )
        {
            do
            {
                nodeType = xmlReader.readNext();
                if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "history" )
                {
                    while(nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "history")
                    {
                        nodeType = xmlReader.readNext();

                        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "entry" )
                        {
                            SongRequest sr;
                            isOk &= readQueueEntry(xmlReader, sr);
                            sr.timeLeft = 0;
                            history.push_back(sr);
                        }
                    }
                }

            }while((nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "playlist") && isOk);
        }
    }

    qDebug() << history;

    return isOk;
}
